package com.zjxny.utils;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * imagechops 是 Python 的一个图像处理库，它提供了一组用于对图像进行各种操作的函数。在 Java 中，可以使用
 * java.awt.image.BufferedImage 类和 java.awt.image.Raster 类来进行类似的图像处理操作。以下是
 * imagechops 中的几个常用函数以及它们在 Java 中的实现：
 * 
 */
public class ImageChopsUtil {

	public static BufferedImage difference(BufferedImage image1, BufferedImage image2) {
		int width = image1.getWidth();
		int height = image1.getHeight();
		BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				int color1 = image1.getRGB(x, y);
				int color2 = image2.getRGB(x, y);
				int r1 = (color1 >> 16) & 0xFF;
				int g1 = (color1 >> 8) & 0xFF;
				int b1 = color1 & 0xFF;
				int r2 = (color2 >> 16) & 0xFF;
				int g2 = (color2 >> 8) & 0xFF;
				int b2 = color2 & 0xFF;
				int r = Math.abs(r1 - r2);
				int g = Math.abs(g1 - g2);
				int b = Math.abs(b1 - b2);
				int rgb = (r << 16) | (g << 8) | b;
				result.setRGB(x, y, rgb);
			}
		}
		return result;
	}

//	imagechops.multiply(image1, image2, scale=1.0, offset=0)：将两个图像的像素值相乘，并将结果缩放和平移指定的比例和偏移量。同样可以使用 BufferedImage 的 getRGB() 和 setRGB() 方法实现。
	public static BufferedImage multiply(BufferedImage image1, BufferedImage image2, Float scale, Integer offset) {
		int width = image1.getWidth();
		int height = image1.getHeight();
		BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				int color1 = image1.getRGB(x, y);
				int color2 = image2.getRGB(x, y);
				int r1 = (color1 >> 16) & 0xFF;
				int g1 = (color1 >> 8) & 0xFF;
				int b1 = color1 & 0xFF;
				int r2 = (color2 >> 16) & 0xFF;
				int g2 = (color2 >> 8) & 0xFF;
				int b2 = color2 & 0xFF;
				int r = (int) (r1 * r2 / 255.0 * scale + offset);
				int g = (int) (g1 * g2 / 255.0 * scale + offset);
				int b = (int) (b1 * b2 / 255.0 * scale + offset);
				r = Math.min(255, Math.max(0, r));
				g = Math.min(255, Math.max(0, g));
				b = Math.min(255, Math.max(0, b));
				int rgb = (r << 16) | (g << 8) | b;
				result.setRGB(x, y, rgb);
			}
		}
		return result;
	}

//	imagechops.subtract(image1, image2, scale=1.0, offset=0)：将第二个图像的像素值从第一个图像的像素值中减去，并将结果缩放和平移指定的比例和偏移量。同样可以使用 BufferedImage 的 getRGB() 和 setRGB() 方法实现。
	public static BufferedImage subtract(BufferedImage image1, BufferedImage image2, Float scale, Integer offset) {
		int width = image1.getWidth();
		int height = image1.getHeight();
		BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				int color1 = image1.getRGB(x, y);
				int color2 = image2.getRGB(x, y);
				int r1 = (color1 >> 16) & 0xFF;
				int g1 = (color1 >> 8) & 0xFF;
				int b1 = color1 & 0xFF;
				int r2 = (color2 >> 16) & 0xFF;
				int g2 = (color2 >> 8) & 0xFF;
				int b2 = color2 & 0xFF;
				int r = (int) (r1 - r2 * scale + offset);
				int g = (int) (g1 - g2 * scale + offset);
				int b = (int) (b1 - b2 * scale + offset);
				r = Math.min(255, Math.max(0, r));
				g = Math.min(255, Math.max(0, g));
				b = Math.min(255, Math.max(0, b));
				int rgb = (r << 16) | (g << 8) | b;
				result.setRGB(x, y, rgb);
			}
		}

		return result;
	}

	/**
	 * imagechops.add(image1, image2, scale=1.0,
	 * offset=0)：将两个图像的像素值相加，并将结果缩放和平移指定的比例和偏移量。在 Java 中可以使用 BufferedImage 的
	 * getRGB() 和 setRGB() 方法来分别获取和设置每个像素的颜色值。
	 * 
	 * @param image1
	 * @param image2
	 * @param scale
	 * @param offset
	 * @return
	 */
	public static BufferedImage add(BufferedImage image1, BufferedImage image2, Float scale, Integer offset) {
		int width = image1.getWidth();
		int height = image1.getHeight();
		BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				int color1 = image1.getRGB(x, y);
				int color2 = image2.getRGB(x, y);
				int r1 = (color1 >> 16) & 0xFF;
				int g1 = (color1 >> 8) & 0xFF;
				int b1 = color1 & 0xFF;
				int r2 = (color2 >> 16) & 0xFF;
				int g2 = (color2 >> 8) & 0xFF;
				int b2 = color2 & 0xFF;
				int r = (int) (r1 - r2 * scale + offset);
				int g = (int) (g1 - g2 * scale + offset);
				int b = (int) (b1 - b2 * scale + offset);
				r = Math.min(255, Math.max(0, r));
				g = Math.min(255, Math.max(0, g));
				b = Math.min(255, Math.max(0, b));
				int rgb = (r << 16) | (g << 8) | b;
				result.setRGB(x, y, rgb);
			}
		}
		return result;
	}
	
	public static void main(String[] args) throws Exception {
		File file = new File("./data/1002.png");
		File file2 = new File("./data/10021.png");
        BufferedImage image1 = ImageIO.read(file);
        BufferedImage image2 = ImageIO.read(file2);
        
        BufferedImage image3 = difference(image1,image2);
        File outputFile = new File("./data/output.png");
        ImageIO.write(image3, "png", outputFile);
	}
}
